﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for OrderDetailController
/// </summary>
public class OrderDetailController:Interface
{
    private SqlConnection con;
	public OrderDetailController()
	{
	    con = ConnectDB.Connect();
        ConnectDB.Open();
	}

    public int Insert(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Insert_OrderDetail";
            cmd.CommandType = CommandType.StoredProcedure;
            OrderDetail orders = (OrderDetail)obj;
            cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = orders.Product_id;
            cmd.Parameters.Add("@order_id", SqlDbType.Int).Value = orders.Order_id;
            cmd.Parameters.Add("@quantity", SqlDbType.Int).Value = orders.Quantity;
            cmd.Parameters.Add("@price", SqlDbType.Float).Value = orders.Price;
            cmd.Parameters.Add("@description", SqlDbType.NText).Value = orders.Description;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = orders.Status;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Update(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Update_OrderDetail";
            cmd.CommandType = CommandType.StoredProcedure;
            OrderDetail orders = (OrderDetail)obj;
            cmd.Parameters.Add("@order_detail_id", SqlDbType.Int).Value = orders.OrderDetail_id;
            cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = orders.Product_id;
            cmd.Parameters.Add("@order_id", SqlDbType.Int).Value = orders.Order_id;
            cmd.Parameters.Add("@quantity", SqlDbType.Int).Value = orders.Quantity;
            cmd.Parameters.Add("@price", SqlDbType.Float).Value = orders.Price;
            cmd.Parameters.Add("@description", SqlDbType.NText).Value = orders.Description;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = orders.Status;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Delete(int id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Delete_OrderDetail";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@order_detail_id", SqlDbType.Int).Value = id;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public DataTable GetAll()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_OrderDetail ORDER BY product_id ASC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {
            return null;
            throw;
        }
    }

    public DataTable GetByID(int id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_OrderDetail WHERE order_detail_id=@order_detail_id ORDER BY product_id ASC";
            cmd.Parameters.AddWithValue("@order_detail_id", id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {
            return null;
            throw;
        }
    }
    public DataTable GetByOrderID(int order_id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_OrderDetailByOrder WHERE order_id=@order_id ORDER BY product_id ASC";
            cmd.Parameters.AddWithValue("@order_id", order_id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {
            return null;
            throw;
        }
    }
}